/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginDlg.java
 *
 * Created on May 5, 2010, 12:04:44 AM
 */
package com.aptech.childcare.gui;

import com.aptech.childcare.buz.ChildcareBuz;
import com.aptech.childcare.entities.Staff;
import com.jgoodies.looks.plastic.theme.ExperienceRoyale;
import com.nilo.plaf.nimrod.NimRODLookAndFeel;
import java.awt.Image;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author BaoTien
 */
public class LoginDlg extends java.awt.Dialog {

    MainFrm main;

    /** Creates new form LoginDlg */
    public LoginDlg(MainFrm main, boolean modal) {
        super(main, modal);
        this.main = main;

        try {
            ImageIcon icon = new ImageIcon(getClass().getResource("../icon/logo.png"));
            Image image = icon.getImage();
            setIconImage(image);
            NimRODLookAndFeel nim = new NimRODLookAndFeel();
            NimRODLookAndFeel.setCurrentTheme(new ExperienceRoyale());
            UIManager.setLookAndFeel(nim);
            SwingUtilities.updateComponentTreeUI(this);
            initComponents();
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(LoginDlg.class.getName()).log(Level.SEVERE, null, ex);
        }
        setLocationRelativeTo(this);
    }

    private void setEnabledAdmin() {
        main.btnAddStaff.setEnabled(true);
        main.btnAddChild.setEnabled(true);
        main.btnAddNanny.setEnabled(true);
        main.btnEditclass.setEnabled(true);
        main.btnEdit.setEnabled(true);
        main.btnEditActivities.setEnabled(true);
        main.btnEditChild.setEnabled(true);
        main.btnEditNanny.setEnabled(true);
        main.btnEditStaff.setEnabled(true);
        main.btnSaveActivities.setEnabled(true);
        main.btnSaveAgegroup.setEnabled(true);
        main.btnSaveClass.setEnabled(true);
        main.btnDelete.setEnabled(true);
        main.btnDeleteActivities.setEnabled(true);
        main.btnDeleteChild.setEnabled(true);
        main.btnDeleteClass.setEnabled(true);
        main.btnDeleteStaff.setEnabled(true);
        main.btnDeleteNanny.setEnabled(true);
        main.btnSetAct.setEnabled(true);
        main.itmChangePass.setEnabled(true);
        main.itmLogin.setEnabled(false);
        main.itmEditInformation.setEnabled(true);
        main.itmDataConnection.setEnabled(true);

    }

    private void setEnabledStaff() {
        main.btnAddChild.setEnabled(true);
        main.btnEditclass.setEnabled(true);
        main.btnEditChild.setEnabled(true);
        main.btnDeleteChild.setEnabled(true);
        main.btnDeleteClass.setEnabled(true);
        main.btnSaveClass.setEnabled(true);
        main.btnSetAct.setEnabled(true);
        main.itmChangePass.setEnabled(true);
        main.itmLogin.setEnabled(false);
        main.itmEditInformation.setEnabled(true);

    }

    private void setEnabled() {
        main.btnAddStaff.setEnabled(false);
        main.btnAddChild.setEnabled(false);
        main.btnAddNanny.setEnabled(false);
        main.btnEditclass.setEnabled(false);
        main.btnEdit.setEnabled(false);
        main.btnEditActivities.setEnabled(false);
        main.btnEditChild.setEnabled(false);
        main.btnEditNanny.setEnabled(false);
        main.btnEditStaff.setEnabled(false);
        main.btnSaveActivities.setEnabled(false);
        main.btnSaveAgegroup.setEnabled(false);
        main.btnSaveClass.setEnabled(false);
        main.btnDelete.setEnabled(false);
        main.btnDeleteActivities.setEnabled(false);
        main.btnDeleteChild.setEnabled(false);
        main.btnDeleteClass.setEnabled(false);
        main.btnDeleteStaff.setEnabled(false);
        main.btnDeleteNanny.setEnabled(false);
        main.btnSetAct.setEnabled(false);
        main.itmChangePass.setEnabled(false);
        main.itmEditInformation.setEnabled(false);
    }

    /**
     * 
     */
    private void login() throws IOException {
        Staff sta = new Staff();
        ChildcareBuz buz = new ChildcareBuz();
        sta.setUserName(txtUserName.getText());
        sta.setPassWord(String.valueOf(pafPassword.getPassword()));
        if (rbAdministrators.isSelected()) {
            sta.setPosition("Administrators");
            setEnabledAdmin();
        }
        if (rbStaff.isSelected()) {
            sta.setPosition("Staff");
            setEnabledStaff();
        }
        if (buz.checkAccStaff(sta)) {
            main.lbOnline.setText(sta.getUserName());
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, buz.stdao.getLastError());
            setEnabled();
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        txtUserName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        pafPassword = new javax.swing.JPasswordField();
        jPanel4 = new javax.swing.JPanel();
        rbAdministrators = new javax.swing.JRadioButton();
        rbStaff = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnLogin = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setAlwaysOnTop(true);
        setResizable(false);
        setTitle("Childcare Management System");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel1.setNextFocusableComponent(txtUserName);
        jPanel1.setRequestFocusEnabled(false);
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Account", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(102, 102, 255))); // NOI18N
        jPanel2.setLayout(new java.awt.GridBagLayout());

        txtUserName.setColumns(15);
        txtUserName.setNextFocusableComponent(pafPassword);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(txtUserName, gridBagConstraints);

        jLabel3.setText("Password");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(jLabel3, gridBagConstraints);

        jLabel2.setText("UserName");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(jLabel2, gridBagConstraints);

        pafPassword.setColumns(15);
        pafPassword.setNextFocusableComponent(btnLogin);
        pafPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                pafPasswordKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(pafPassword, gridBagConstraints);

        buttonGroup1.add(rbAdministrators);
        rbAdministrators.setSelected(true);
        rbAdministrators.setText("Administrators");
        jPanel4.add(rbAdministrators);

        buttonGroup1.add(rbStaff);
        rbStaff.setText("Staff");
        jPanel4.add(rbStaff);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        jPanel2.add(jPanel4, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 12);
        jPanel1.add(jPanel2, gridBagConstraints);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Login");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 12);
        jPanel1.add(jLabel1, gridBagConstraints);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/childcare/icon/user1_monitor.png"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel1.add(jLabel4, gridBagConstraints);

        btnLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/childcare/icon/lock_add.png"))); // NOI18N
        btnLogin.setText("Login");
        btnLogin.setMaximumSize(new java.awt.Dimension(85, 25));
        btnLogin.setMinimumSize(new java.awt.Dimension(85, 25));
        btnLogin.setNextFocusableComponent(btnCancel);
        btnLogin.setPreferredSize(new java.awt.Dimension(85, 25));
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        jPanel3.add(btnLogin);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/childcare/icon/Cancel-2-16x16.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.setNextFocusableComponent(txtUserName);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel3.add(btnCancel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 15, 5);
        jPanel1.add(jPanel3, gridBagConstraints);

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        txtUserName.setText("");
        pafPassword.setText("");
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        try {
            // TODO add your handling code here:
            login();
        } catch (IOException ex) {
            Logger.getLogger(LoginDlg.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void pafPasswordKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pafPasswordKeyReleased
        // TODO add your handling code here:
        if (evt.getKeyCode() == evt.VK_ENTER) {
            try {
                login();
            } catch (IOException ex) {
                Logger.getLogger(LoginDlg.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_pafPasswordKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    LoginDlg dialog = new LoginDlg(new MainFrm(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(LoginDlg.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnLogin;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPasswordField pafPassword;
    private javax.swing.JRadioButton rbAdministrators;
    private javax.swing.JRadioButton rbStaff;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
